package com.ryan.spring.annotation.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

/**
 * Created by kaimin on 29/1/2019.
 * time : 10:06
 */
@Aspect
public class LogAspect {

    @Pointcut("execution(public int com.ryan.spring.annotation.aop.MathCal.*(..))")
    public void pointCut(){

    }

//    @Before("pointCut()")
    public void logStart(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        System.out.println(joinPoint.getSignature().getName()+"日志开始,传入进来的参数是："+ Arrays.asList(args));
    }
    @After("pointCut()")
    public void logEnd(JoinPoint joinPoint){
        System.out.println(joinPoint.getSignature().getName()+"日志结束");
    }

//    @AfterThrowing(value = "pointCut()",throwing = "exception")//让exception来接
    public void logException(JoinPoint joinPoint,Exception exception){
        System.out.println(joinPoint.getSignature().getName()+"业务出现异常，异常信息是："+exception);
    }

//    @AfterReturning(value = "pointCut()",returning = "result")
    public void logReturn(JoinPoint joinPoint,Object result){
        System.out.println(joinPoint.getSignature().getName()+"业务返回结果:"+result);
    }

//    @Around(value = "pointCut()")
    public Object logAround(ProceedingJoinPoint joinPoint){
        System.out.println(joinPoint.getSignature().getName()+"业务环绕开始");
        try {
            Object proceed = joinPoint.proceed();
            System.out.println(joinPoint.getSignature().getName()+"业务环绕结束");
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

}
